



<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#FFF">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon.png">

<link rel="icon" type="image/ico" sizes="32x32" href="/blog/images/favicon.ico">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">


<link rel="alternate" type="application/rss+xml" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/rss.xml" />
<link rel="alternate" type="application/atom+xml" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/atom.xml" />
<link rel="alternate" type="application/json" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/feed.json" />

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Mulish:300,300italic,400,400italic,700,700italic%7CFredericka%20the%20Great:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20JP:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20SC:300,300italic,400,400italic,700,700italic%7CInconsolata:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/blog/css/app.css?v=0.2.5">

  
  <meta name="keywords" content="分布式" />


<link rel="canonical" href="http://fyduyu.gitee.io/blog/2020/12/24/Nacos/">



  <title>
Cloud Alibaba Nacos - Spring Cloud Alibaba |
blog = 凡尘一叶</title>
<meta name="generator" content="Hexo 5.3.0"></head>
<body itemscope itemtype="http://schema.org/WebPage">
  <div id="loading">
    <div class="cat">
      <div class="body"></div>
      <div class="head">
        <div class="face"></div>
      </div>
      <div class="foot">
        <div class="tummy-end"></div>
        <div class="bottom"></div>
        <div class="legs left"></div>
        <div class="legs right"></div>
      </div>
      <div class="paw">
        <div class="hands left"></div>
        <div class="hands right"></div>
      </div>
    </div>
  </div>
  <div id="container">
    <header id="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="inner">
        <div id="brand">
          <div class="pjax">
          
  <h1 itemprop="name headline">Cloud Alibaba Nacos
  </h1>
  
<div class="meta">
  <span class="item" title="创建时间：2020-12-24 22:32:22">
    <span class="icon">
      <i class="ic i-calendar"></i>
    </span>
    <span class="text">发表于</span>
    <time itemprop="dateCreated datePublished" datetime="2020-12-24T22:32:22+08:00">2020-12-24</time>
  </span>
  <span class="item" title="本文字数">
    <span class="icon">
      <i class="ic i-pen"></i>
    </span>
    <span class="text">本文字数</span>
    <span>7.2k</span>
    <span class="text">字</span>
  </span>
  <span class="item" title="阅读时长">
    <span class="icon">
      <i class="ic i-clock"></i>
    </span>
    <span class="text">阅读时长</span>
    <span>7 分钟</span>
  </span>
</div>


          </div>
        </div>
        <nav id="nav">
  <div class="inner">
    <div class="toggle">
      <div class="lines" aria-label="切换导航栏">
        <span class="line"></span>
        <span class="line"></span>
        <span class="line"></span>
      </div>
    </div>
    <ul class="menu">
      <li class="item title"><a href="/blog/" rel="start">blog</a></li>
    </ul>
    <ul class="right">
      <li class="item theme">
        <i class="ic i-sun"></i>
      </li>
      <li class="item search">
        <i class="ic i-search"></i>
      </li>
    </ul>
  </div>
</nav>

      </div>
      <div id="imgs" class="pjax">
        <ul>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1giclh3brzpj20zk0m8ann.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicli9lfebj20zk0m84qp.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gipewkhf1zj20zk0m81kx.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1giclhfehz7j20zk0m8u0x.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicliierfjj20zk0m8npd.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicliwyw55j20zk0m8hdt.jpg"></li>
        </ul>
      </div>
    </header>
    <div id="waves">
      <svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto">
        <defs>
          <path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z" />
        </defs>
        <g class="parallax">
          <use xlink:href="#gentle-wave" x="48" y="0" />
          <use xlink:href="#gentle-wave" x="48" y="3" />
          <use xlink:href="#gentle-wave" x="48" y="5" />
          <use xlink:href="#gentle-wave" x="48" y="7" />
        </g>
      </svg>
    </div>
    <main>
      <div class="inner">
        <div id="main" class="pjax">
          
  <div class="article wrap">
    
<div class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
<i class="ic i-home"></i>
<span><a href="/blog/">首页</a></span><i class="ic i-angle-right"></i>
<span  class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="/blog/categories/Spring-Cloud-Alibaba/" itemprop="item" rel="index" title="分类于 Spring Cloud Alibaba"><span itemprop="name">Spring Cloud Alibaba</span></a>
<meta itemprop="position" content="1" /></span>
</div>

    <article itemscope itemtype="http://schema.org/Article" class="post block" lang="zh-CN">
  <link itemprop="mainEntityOfPage" href="http://fyduyu.gitee.io/blog/2020/12/24/Nacos/">

  <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="image" content="/blog/images/touxiang.jpg">
    <meta itemprop="name" content="杜朝宇">
    <meta itemprop="description" content=", 个人博客&编程笔记">
  </span>

  <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="凡尘一叶">
  </span>

  <div class="body md" itemprop="articleBody">
    

    <h1 id="安装nacos"><a class="markdownIt-Anchor" href="#安装nacos">#</a> 安装 Nacos:</h1>
<p><strong>官网</strong>：</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9uYWNvcy5pby96aC1jbi9kb2NzL3F1aWNrLXN0YXJ0Lmh0bWw=">https://nacos.io/zh-cn/docs/quick-start.html</span></p>
<p><strong>Nacos 支持 ap 和 cp 模式的切换</strong></p>
<p>需要 java8  和 Mavne</p>
<p><strong>1, 到 github 上下载安装包</strong></p>
<p>​		解压安装包</p>
<p><strong>2, 启动 Nacos</strong></p>
<p>​		在 bin 下，进入 cod</p>
<p>​		cmd startup.cmd -m standalone</p>
<p><strong>3 访问 Nacos</strong></p>
<p>​		Nacos 默认监听 8848</p>
<p>​		localhost:8848/nacos</p>
<p>​		账号密码：默认都是 nacos</p>
<h1 id="使用nacos"><a class="markdownIt-Anchor" href="#使用nacos">#</a> 使用 Nacos</h1>
<h2 id="nacos提供者"><a class="markdownIt-Anchor" href="#nacos提供者">#</a> Nacos 提供者</h2>
<h3 id="pom"><a class="markdownIt-Anchor" href="#pom">#</a> POM</h3>
<figure class="highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr></table></figure><p>父依赖 Dependency Management</p>
<figure class="highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencyManagement</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-alibaba-dependencies<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.1.BUILD-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="7"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>type</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>type</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>import<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencyManagement</span><span class="token punctuation">></span></span></pre></td></tr></table></figure><h3 id="配置文件"><a class="markdownIt-Anchor" href="#配置文件">#</a> 配置文件</h3>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9001</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> nacos<span class="token punctuation">-</span>provider <span class="token comment">#提供者名字</span></pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 127.0.0.1<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务端</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token key atrule">management</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>  <span class="token key atrule">endpoints</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token key atrule">web</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>      <span class="token key atrule">exposure</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token key atrule">include</span><span class="token punctuation">:</span> <span class="token string">"*"</span>  <span class="token comment">#暴漏接口</span></pre></td></tr></table></figure><h3 id="启动类-和-controller"><a class="markdownIt-Anchor" href="#启动类-和-controller">#</a> 启动类  和 controller</h3>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@EnableDiscoveryClient</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NacosProviderDemoApplication</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">NacosProviderDemoApplication</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&#123;server.port&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">String</span> serveport<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">EchoController</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/echo/&#123;string&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">echo</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span> <span class="token class-name">String</span> string<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>            <span class="token keyword">return</span> <span class="token string">"Hello Nacos Discovery "</span> <span class="token operator">+</span> string<span class="token operator">+</span><span class="token string">" ****port:   "</span><span class="token operator">+</span>serveport<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><p>启动 9001</p>
<p>然后查看 Nacos 的 web 界面，可以看到 9001 已经注册成功</p>
<h2 id="nacos消费者"><a class="markdownIt-Anchor" href="#nacos消费者">#</a> Nacos 消费者</h2>
<h3 id="pom-2"><a class="markdownIt-Anchor" href="#pom-2">#</a> pom</h3>
<p>和提供者一样</p>
<figure class="highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr></table></figure><h3 id="配置文件-2"><a class="markdownIt-Anchor" href="#配置文件-2">#</a> 配置文件</h3>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">83</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> nacos<span class="token punctuation">-</span>order<span class="token punctuation">-</span>consumer  <span class="token comment">#消费者名字</span></pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 127.0.0.1<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务端</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token comment">#提供者的 url</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token key atrule">server-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>  <span class="token key atrule">nacos-user-service</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//nacos<span class="token punctuation">-</span>provider</pre></td></tr></table></figure><h3 id="主启动类和controller"><a class="markdownIt-Anchor" href="#主启动类和controller">#</a> 主启动类和 controller</h3>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@EnableDiscoveryClient</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NacosConsumerApp</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NacosController</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token annotation punctuation">@Autowired</span></pre></td></tr><tr><td data-num="10"></td><td><pre>        <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&#123;server-url.nacos-user-service&#125;"</span><span class="token punctuation">)</span> <span class="token comment">//yml 设置的提供者地址</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token keyword">private</span> <span class="token class-name">String</span> serverURL<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/consumer/nacos/&#123;str&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">echoAppName</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">"str"</span><span class="token punctuation">)</span><span class="token class-name">String</span> str<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>            <span class="token keyword">return</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span>serverURL<span class="token operator">+</span><span class="token string">"/echo/"</span><span class="token operator">+</span>str<span class="token punctuation">,</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="19"></td><td><pre></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="21"></td><td><pre></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token comment">//Instantiate RestTemplate Instance</span></pre></td></tr><tr><td data-num="23"></td><td><pre>    <span class="token annotation punctuation">@Bean</span></pre></td></tr><tr><td data-num="24"></td><td><pre>    <span class="token annotation punctuation">@LoadBalanced</span>  <span class="token comment">// 负载均衡</span></pre></td></tr><tr><td data-num="25"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">RestTemplate</span> <span class="token function">restTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="26"></td><td><pre>        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">RestTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="27"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="28"></td><td><pre></pre></td></tr><tr><td data-num="29"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="30"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">NacosConsumerApp</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="31"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="32"></td><td><pre></pre></td></tr><tr><td data-num="33"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h3 id="测试"><a class="markdownIt-Anchor" href="#测试">#</a> 测试</h3>
<p>启动 83, 访问 9001,9002, 可以看到，实现了负载均衡</p>
<h1 id="使用nacos作为配置中心"><a class="markdownIt-Anchor" href="#使用nacos作为配置中心">#</a> 使用 Nacos 作为配置中心</h1>
<h2 id="创建配置中心的客户端模块"><a class="markdownIt-Anchor" href="#创建配置中心的客户端模块">#</a> 创建配置中心的客户端模块</h2>
<h3 id="pom-3"><a class="markdownIt-Anchor" href="#pom-3">#</a> pom</h3>
<figure class="highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">&lt;!-- nacos config--></span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-config<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token comment">&lt;!-- SpringCloud ailibaba nacos--></span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr></table></figure><h3 id="配置文件-3"><a class="markdownIt-Anchor" href="#配置文件-3">#</a> 配置文件</h3>
<p>这里需要配置两个配置文件，application.ymk 和 bootstrap.yml</p>
<p>​			主要是为了可以与 spring clodu config 无缝迁移</p>
<p><strong>bootstrap.yml</strong></p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3377</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> nacos<span class="token punctuation">-</span>config<span class="token punctuation">-</span>client       <span class="token comment">#配置的开始名字</span></pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务注册中心</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务配置中心</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token key atrule">file-extension</span><span class="token punctuation">:</span> yml       <span class="token comment">#配置的格式</span></pre></td></tr></table></figure><p><strong>application.yml</strong></p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">profiles</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">active</span><span class="token punctuation">:</span> dev  <span class="token comment">#表示开发环境 需要选择的环境</span></pre></td></tr></table></figure><h3 id="主启动类和controller-2"><a class="markdownIt-Anchor" href="#主启动类和controller-2">#</a> 主启动类和 controller</h3>
<p>通过 Spring Cloud 原生注解  <code>@RefreshScope</code>  实现配置自动更新：</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@EnableDiscoveryClient</span>  <span class="token comment">// 开启 config</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NacosConfigClientMain3377</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">NacosConfigClientMain3377</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token annotation punctuation">@RefreshScope</span>  <span class="token comment">// 支持 Nacos 的动态刷新功能</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfigClientController</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&#123;config.info&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token keyword">private</span> <span class="token class-name">String</span> configInfo<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/config/Info"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getConfigInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>            <span class="token keyword">return</span> configInfo<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="19"></td><td><pre></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h3 id="在nacos添加配置信息"><a class="markdownIt-Anchor" href="#在nacos添加配置信息">#</a> 在 Nacos 添加配置信息</h3>
<p file-extension=""><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>p</mi><mi>r</mi><mi>e</mi><mi>f</mi><mi>i</mi><mi>x</mi></mrow><mo>−</mo></mrow><annotation encoding="application/x-tex">{prefix}-</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal">x</span></span><span class="mord">−</span></span></span></span>{spring.profiles.active}.$</p>
<p>prefix:<br>
 默认就是当前服务的服务名称<br>
也可以通过 spring.cloud.necos.config.prefix 配置<br>
 spring.profile.active:<br>
 就是我们在 application.yml 中指定的，当前是开发环境还是测试等环境<br>
这个可以不配置，如果不配置，那么前面的 -  也会没有<br>
 file-extension<br>
 就是当前文件的格式 (后缀), 目前只支持 yml 和 properties</p>
<h3 id="测试-2"><a class="markdownIt-Anchor" href="#测试-2">#</a> 测试</h3>
<p>重启 3377 客户端</p>
<p>访问 3377</p>
<p>拿到了配置文件中的值</p>
<p><strong>注意默认就开启了自动刷新</strong></p>
<p>此时我们修改了配置文件</p>
<p>客户端是可以立即更新的</p>
<p>​			因为 Nacos 支持 Bus 总线，会自动发送命令更新所有客户端</p>
<h2 id="nacos配置中心之分类配置"><a class="markdownIt-Anchor" href="#nacos配置中心之分类配置">#</a> Nacos 配置中心之分类配置</h2>
<p>NameSpace 默认有一个:public 名称空间</p>
<p>这三个类似 java 的：包名 + 类名 + 方法名</p>
<h3 id="配置不同dataid"><a class="markdownIt-Anchor" href="#配置不同dataid">#</a> 配置不同 DataId</h3>
<p>​	<mark>通过配置文件，实现多环境的读取:</mark></p>
<p>在 application.yml</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">profiles</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">active</span><span class="token punctuation">:</span> test  <span class="token comment">#表示开发环境</span></pre></td></tr></table></figure><h3 id="配置不同的groupid"><a class="markdownIt-Anchor" href="#配置不同的groupid">#</a> 配置不同的 GroupID</h3>
<p>直接在新建配置文件时指定组</p>
<p><mark>在客户端配置，使用指定组的配置文件:</mark></p>
<p>bootstrap.yml 新增加</p>
<p><strong>spring.cloud.nacos.config.group</strong></p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3377</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> nacos<span class="token punctuation">-</span>config<span class="token punctuation">-</span>client       <span class="token comment">#配置的开始名字</span></pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务注册中心</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务配置中心</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token key atrule">file-extension</span><span class="token punctuation">:</span> yml       <span class="token comment">#配置的格式</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token key atrule">group</span><span class="token punctuation">:</span> TEST_GROUP  <span class="token comment">#需要分配的组</span></pre></td></tr></table></figure><p>重启服务，即可</p>
<h3 id="配置不同的namespace"><a class="markdownIt-Anchor" href="#配置不同的namespace">#</a> 配置不同的 namespace</h3>
<p><mark>客户端配置使用不同名称空间:</mark></p>
<p>bootstrap.yml 新增加</p>
<p><strong>spring.cloud.nacos.config.namespace</strong></p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3377</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> nacos<span class="token punctuation">-</span>config<span class="token punctuation">-</span>client       <span class="token comment">#配置的开始名字</span></pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务注册中心</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>  <span class="token comment">#nacos 服务配置中心</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token key atrule">file-extension</span><span class="token punctuation">:</span> yml       <span class="token comment">#配置的格式</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token key atrule">group</span><span class="token punctuation">:</span> TEST_GROUP  <span class="token comment">#需要分配的组</span></pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token key atrule">namespace</span><span class="token punctuation">:</span> 2fb6cab3<span class="token punctuation">-</span>f252<span class="token punctuation">-</span>4354<span class="token punctuation">-</span>a0b3<span class="token punctuation">-</span>758fd507c906</pre></td></tr></table></figure><h1 id="nacos集群和持久化配置"><a class="markdownIt-Anchor" href="#nacos集群和持久化配置">#</a> Nacos 集群和持久化配置:</h1>
<h2 id="安装"><a class="markdownIt-Anchor" href="#安装">#</a> 安装</h2>
<p><strong>访问 https://github.com/alibaba/nacos/tags 进行 nacos 的下载，版本自行选择。</strong></p>
<p><strong>下载完成后将 .tar.gz 后缀文件放到 linux 的 /opt 目录下。</strong></p>
<p><strong>进入 opt 目录进行解压 (这里是 1.1.4 版本)。</strong></p>
<p>tar -zxvf nacos-server-1.1.4.tar.gz</p>
<p><strong>解压完成后将 nacos 文件夹移动到 mynacos 目录下，方便查看。</strong></p>
<p>cp -r nacos /mynacos</p>
<p><strong>安装完成</strong></p>
<h2 id="切换mysql数据库"><a class="markdownIt-Anchor" href="#切换mysql数据库">#</a> 切换 mysql 数据库</h2>
<figure class="highlight properties"><figcaption data-lang=".properties"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">#链接并创建数据库</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token attr-name">mysql</span> <span class="token attr-value">-u root -p'SYS_666_admin' -h 192.168.44.191 -P 3306</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token attr-name">create</span> <span class="token attr-value">database nacos_config;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment">#切换目录并导入脚本</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token attr-name">cd</span> <span class="token attr-value">/opt/module/nacos/conf &amp;&amp; ll</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token attr-name">mysql</span> <span class="token attr-value">-uroot -h 192.168.44.191 -p'SYS_666_admin' -P 3306 nacos_config &lt; nacos-mysql.sql</span></pre></td></tr></table></figure><p><strong>进入 conf 目录下将 目录内的 nacos-mysql.sql 文件放到数据库中执行。</strong></p>
<p><strong>修改 nacos 配置文件将其从原来的 derby 转换为 mysql。先将配置文件备份。</strong></p>
<figure class="highlight properties"><figcaption data-lang=".properties"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token attr-name">cd</span> <span class="token attr-value">/mynacos/conf/</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token attr-name">cp</span> <span class="token attr-value">application.properties application.properties.init</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token attr-name">vim</span> <span class="token attr-value">application.properties</span></pre></td></tr></table></figure><figure class="highlight properties"><figcaption data-lang=".properties"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">#切换目录</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token attr-name">cd</span> <span class="token attr-value">/opt/module/nacos/conf</span></pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token comment">#编辑</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token attr-name">vim</span> <span class="token attr-value">application.properties</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token comment">#加入内容【根据实际填写】</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment"># 表明用 MySQL 作为后端存储</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token attr-name">spring.datasource.platform</span><span class="token punctuation">=</span><span class="token attr-value">mysql</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token comment"># 有几个数据库实例</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token attr-name">db.num</span><span class="token punctuation">=</span><span class="token attr-value">1</span></pre></td></tr><tr><td data-num="13"></td><td><pre></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token comment"># 第 1 个实例【从 0 开始】</span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token attr-name">db.url.0</span><span class="token punctuation">=</span><span class="token attr-value">jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf-8&amp;connectTimeout=1000&amp;socketTimeout=3000&amp;autoReconnect=true</span></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token attr-name">db.user</span><span class="token punctuation">=</span><span class="token attr-value">root #账号</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token attr-name">db.password</span><span class="token punctuation">=</span><span class="token attr-value">root #密码</span></pre></td></tr></table></figure><h2 id="修改clusterconf-文件进行集群配置"><a class="markdownIt-Anchor" href="#修改clusterconf-文件进行集群配置">#</a> 修改 cluster.conf 文件，进行集群配置</h2>
<figure class="highlight properties"><figcaption data-lang=".properties"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">#切换目录</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token attr-name">cd</span> <span class="token attr-value">/opt/module/nacos/conf</span></pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token comment">#拷贝文件</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token attr-name">cp</span> <span class="token attr-value">cluster.conf.example cluster.conf</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token comment">#编辑</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token attr-name">vim</span> <span class="token attr-value">cluster.conf</span></pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token comment">#加入节点信息</span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token attr-name">192.168.44.191</span><span class="token punctuation">:</span><span class="token attr-value">8848</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token attr-name">192.168.44.192</span><span class="token punctuation">:</span><span class="token attr-value">8848</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token attr-name">192.168.44.193</span><span class="token punctuation">:</span><span class="token attr-value">8848</span></pre></td></tr></table></figure><p>将自带的几个 ip 全部注释掉 在下面填写 ip 地址</p>
<h3 id="4-修改nacos启动脚本startupsh让其能够进行不同端口的启动-为了能在一台主机上启动多个实例对象模拟3台主机-有多台主机不需要修改跳过该步骤"><a class="markdownIt-Anchor" href="#4-修改nacos启动脚本startupsh让其能够进行不同端口的启动-为了能在一台主机上启动多个实例对象模拟3台主机-有多台主机不需要修改跳过该步骤">#</a> 4. 修改 nacos 启动脚本 startup.sh 让其能够进行不同端口的启动。(为了能在一台主机上启动多个实例对象，模拟 3 台主机。有多台主机不需要修改，跳过该步骤)</h3>
<figure class="highlight properties"><figcaption data-lang=".properties"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token attr-name">while</span> <span class="token attr-value">getopts ":m:f:s:c:p:p1" opt</span></pre></td></tr><tr><td data-num="2"></td><td><pre>do</pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token attr-name">    case</span> <span class="token attr-value">$opt in</span></pre></td></tr><tr><td data-num="4"></td><td><pre>        m)</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token attr-name">            MODE</span><span class="token punctuation">=</span><span class="token attr-value">$OPTARG;;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        f)</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token attr-name">            FUNCTION_MODE</span><span class="token punctuation">=</span><span class="token attr-value">$OPTARG;;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>        s)</pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token attr-name">            SERVER</span><span class="token punctuation">=</span><span class="token attr-value">$OPTARG;;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>        c)</pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token attr-name">            MEMBER_LIST</span><span class="token punctuation">=</span><span class="token attr-value">$OPTARG;;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        p)</pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token attr-name">            EMBEDDED_STORAGE</span><span class="token punctuation">=</span><span class="token attr-value">$OPTARG;;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>        p1)</pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token attr-name">            PORT</span><span class="token punctuation">=</span><span class="token attr-value">$OPTARG;;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        ?)</pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token attr-name">        echo</span> <span class="token attr-value">"Unknown parameter"</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token attr-name">        exit</span> <span class="token attr-value">1;;</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    esac</pre></td></tr><tr><td data-num="20"></td><td><pre>done</pre></td></tr><tr><td data-num="21"></td><td><pre></pre></td></tr><tr><td data-num="22"></td><td><pre><span class="token attr-name"> JAVA_OPT</span><span class="token punctuation">=</span><span class="token attr-value">"$&#123;JAVA_OPT&#125; -server -Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"</span></pre></td></tr></table></figure><p PORT="">在最底端如图红色框位置加上 -Dserver.port=$</p>
<figure class="highlight properties"><figcaption data-lang=".properties"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment"># start</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token attr-name">echo</span> <span class="token attr-value">"$JAVA $&#123;JAVA_OPT&#125;" > $&#123;BASE_DIR&#125;/logs/start.out 2>&amp;1 &amp;</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token attr-name">nohup</span> <span class="token attr-value">$JAVA -Dserver.port=$&#123;PORT&#125;$&#123;JAVA_OPT&#125; nacos.nacos >> $&#123;BASE_DIR&#125;/logs/start.out 2>&amp;1 &amp;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token attr-name">echo</span> <span class="token attr-value">"nacos is starting，you can check the $&#123;BASE_DIR&#125;/logs/start.out"</span></pre></td></tr></table></figure><h2 id="配置nginx"><a class="markdownIt-Anchor" href="#配置nginx">#</a> 配置 NGINX</h2>
<figure class="highlight properties"><figcaption data-lang=".properties"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token attr-name">cd</span> <span class="token attr-value">/usr/lcoal/nginx/conf  #本地nginx目录</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token attr-name">vim</span> <span class="token attr-value">nginx.conf  #nginx配置文件</span></pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token attr-name">unstream</span> <span class="token attr-value">cluster &#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token attr-name">	server</span> <span class="token attr-value">127.0.0.1:3333;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token attr-name">	server</span> <span class="token attr-value">127.0.0.1:4444;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token attr-name">	server</span> <span class="token attr-value">127.0.0.1:5555;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>&#125;</pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token attr-name">			location</span> <span class="token attr-value">/ &#123;</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token attr-name">				proxy_pass</span> <span class="token attr-value">http://cluster;</span></pre></td></tr><tr><td data-num="13"></td><td><pre>			&#125;</pre></td></tr></table></figure><h2 id="启动"><a class="markdownIt-Anchor" href="#启动">#</a> 启动</h2>
<h3 id="启动nacos"><a class="markdownIt-Anchor" href="#启动nacos">#</a> 启动 Nacos:</h3>
<p>​			./startup.sh -p 3333</p>
<p>​			./startup.sh -p 4444</p>
<p>​			./startup.sh -p 5555</p>
<h3 id="启动nginx"><a class="markdownIt-Anchor" href="#启动nginx">#</a> 启动 nginx</h3>
<h3 id="测试-3"><a class="markdownIt-Anchor" href="#测试-3">#</a> 测试:</h3>
<p>​		访问 192.168.159.121:1111</p>
<p>​		如果可以进入 nacos 的 web 界面，就证明安装成功了</p>
<h2 id="将微服务注册到nacos集群"><a class="markdownIt-Anchor" href="#将微服务注册到nacos集群">#</a> 将微服务注册到 Nacos 集群</h2>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3377</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> nacos<span class="token punctuation">-</span>config<span class="token punctuation">-</span>client       <span class="token comment">#配置的开始名字</span></pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 192.168.44.191<span class="token punctuation">:</span><span class="token number">1111</span>  <span class="token comment">#换成 nginx 的即可</span></pre></td></tr></table></figure>
      <div class="tags">
          <a href="/blog/tags/%E5%88%86%E5%B8%83%E5%BC%8F/" rel="tag"><i class="ic i-tag"></i> 分布式</a>
      </div>
  </div>

   <footer>

    <div class="meta">
  <span class="item">
    <span class="icon">
      <i class="ic i-calendar-check"></i>
    </span>
    <span class="text">更新于</span>
    <time title="修改时间：2020-12-25 14:45:43" itemprop="dateModified" datetime="2020-12-25T14:45:43+08:00">2020-12-25</time>
  </span>
  <span id="2020/12/24/Nacos/" class="item leancloud_visitors" data-flag-title="Cloud Alibaba Nacos" title="阅读次数">
      <span class="icon">
        <i class="ic i-eye"></i>
      </span>
      <span class="text">阅读次数</span>
      <span class="leancloud-visitors-count"></span>
      <span class="text">次</span>
  </span>
</div>

      
<div class="reward">
  <button><i class="ic i-heartbeat"></i> 赞赏</button>
  <p>请我喝[茶]~(￣▽￣)~*</p>
  <div id="qr">
      
      <div>
        <img data-src="/blog/images/wechatpay.png" alt="杜朝宇 微信支付">
        <p>微信支付</p>
      </div>
      
      <div>
        <img data-src="/blog/images/alipay.png" alt="杜朝宇 支付宝">
        <p>支付宝</p>
      </div>
      
      <div>
        <img data-src="/blog/images/paypal.png" alt="杜朝宇 贝宝">
        <p>贝宝</p>
      </div>
  </div>
</div>

      

<div id="copyright">
<ul>
  <li class="author">
    <strong>本文作者： </strong>杜朝宇 <i class="ic i-at"><em>@</em></i>凡尘一叶
  </li>
  <li class="link">
    <strong>本文链接：</strong>
    <a href="http://fyduyu.gitee.io/blog/2020/12/24/Nacos/" title="Cloud Alibaba Nacos">http://fyduyu.gitee.io/blog/2020/12/24/Nacos/</a>
  </li>
  <li class="license">
    <strong>版权声明： </strong>本站所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpo"><i class="ic i-creative-commons"><em>(CC)</em></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

  </footer>

</article>

  </div>
  

<div class="post-nav">
    <div class="item left">
      

  <a href="/blog/2020/12/24/Spring%20Cloud%20Stream/" itemprop="url" rel="prev" data-background-image="https:&#x2F;&#x2F;tva4.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1giclimtf7dj20zk0m8qav.jpg" title="Spring Cloud Stream">
  <span class="type">上一篇</span>
  <span class="category"><i class="ic i-flag"></i> SpringCloud</span>
  <h3>Spring Cloud Stream</h3>
  </a>

    </div>
    <div class="item right">
      

  <a href="/blog/2020/12/24/Spring%20Cloud%20sleuth/" itemprop="url" rel="next" data-background-image="https:&#x2F;&#x2F;tva4.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1giciuja1j1j20zk0m8kjl.jpg" title="Spring Cloud sleuth">
  <span class="type">下一篇</span>
  <span class="category"><i class="ic i-flag"></i> SpringCloud</span>
  <h3>Spring Cloud sleuth</h3>
  </a>

    </div>
</div>

  
  <div class="wrap" id="comments"></div>


        </div>
        <div id="sidebar">
          

<div class="inner">

  <div class="panels">
    <div class="inner">
      <div class="contents panel pjax" data-title="文章目录">
          <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%89%E8%A3%85nacos"><span class="toc-number">1.</span> <span class="toc-text"> 安装 Nacos:</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8nacos"><span class="toc-number">2.</span> <span class="toc-text"> 使用 Nacos</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#nacos%E6%8F%90%E4%BE%9B%E8%80%85"><span class="toc-number">2.1.</span> <span class="toc-text"> Nacos 提供者</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#pom"><span class="toc-number">2.1.1.</span> <span class="toc-text"> POM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">2.1.2.</span> <span class="toc-text"> 配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E7%B1%BB-%E5%92%8C-controller"><span class="toc-number">2.1.3.</span> <span class="toc-text"> 启动类  和 controller</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#nacos%E6%B6%88%E8%B4%B9%E8%80%85"><span class="toc-number">2.2.</span> <span class="toc-text"> Nacos 消费者</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#pom-2"><span class="toc-number">2.2.1.</span> <span class="toc-text"> pom</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-2"><span class="toc-number">2.2.2.</span> <span class="toc-text"> 配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E5%90%AF%E5%8A%A8%E7%B1%BB%E5%92%8Ccontroller"><span class="toc-number">2.2.3.</span> <span class="toc-text"> 主启动类和 controller</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95"><span class="toc-number">2.2.4.</span> <span class="toc-text"> 测试</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8nacos%E4%BD%9C%E4%B8%BA%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83"><span class="toc-number">3.</span> <span class="toc-text"> 使用 Nacos 作为配置中心</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E7%9A%84%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%A8%A1%E5%9D%97"><span class="toc-number">3.1.</span> <span class="toc-text"> 创建配置中心的客户端模块</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#pom-3"><span class="toc-number">3.1.1.</span> <span class="toc-text"> pom</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-3"><span class="toc-number">3.1.2.</span> <span class="toc-text"> 配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E5%90%AF%E5%8A%A8%E7%B1%BB%E5%92%8Ccontroller-2"><span class="toc-number">3.1.3.</span> <span class="toc-text"> 主启动类和 controller</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8nacos%E6%B7%BB%E5%8A%A0%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF"><span class="toc-number">3.1.4.</span> <span class="toc-text"> 在 Nacos 添加配置信息</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95-2"><span class="toc-number">3.1.5.</span> <span class="toc-text"> 测试</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#nacos%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E4%B9%8B%E5%88%86%E7%B1%BB%E9%85%8D%E7%BD%AE"><span class="toc-number">3.2.</span> <span class="toc-text"> Nacos 配置中心之分类配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E4%B8%8D%E5%90%8Cdataid"><span class="toc-number">3.2.1.</span> <span class="toc-text"> 配置不同 DataId</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E4%B8%8D%E5%90%8C%E7%9A%84groupid"><span class="toc-number">3.2.2.</span> <span class="toc-text"> 配置不同的 GroupID</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E4%B8%8D%E5%90%8C%E7%9A%84namespace"><span class="toc-number">3.2.3.</span> <span class="toc-text"> 配置不同的 namespace</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#nacos%E9%9B%86%E7%BE%A4%E5%92%8C%E6%8C%81%E4%B9%85%E5%8C%96%E9%85%8D%E7%BD%AE"><span class="toc-number">4.</span> <span class="toc-text"> Nacos 集群和持久化配置:</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%89%E8%A3%85"><span class="toc-number">4.1.</span> <span class="toc-text"> 安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%87%E6%8D%A2mysql%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">4.2.</span> <span class="toc-text"> 切换 mysql 数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9clusterconf-%E6%96%87%E4%BB%B6%E8%BF%9B%E8%A1%8C%E9%9B%86%E7%BE%A4%E9%85%8D%E7%BD%AE"><span class="toc-number">4.3.</span> <span class="toc-text"> 修改 cluster.conf 文件，进行集群配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E4%BF%AE%E6%94%B9nacos%E5%90%AF%E5%8A%A8%E8%84%9A%E6%9C%ACstartupsh%E8%AE%A9%E5%85%B6%E8%83%BD%E5%A4%9F%E8%BF%9B%E8%A1%8C%E4%B8%8D%E5%90%8C%E7%AB%AF%E5%8F%A3%E7%9A%84%E5%90%AF%E5%8A%A8-%E4%B8%BA%E4%BA%86%E8%83%BD%E5%9C%A8%E4%B8%80%E5%8F%B0%E4%B8%BB%E6%9C%BA%E4%B8%8A%E5%90%AF%E5%8A%A8%E5%A4%9A%E4%B8%AA%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1%E6%A8%A1%E6%8B%9F3%E5%8F%B0%E4%B8%BB%E6%9C%BA-%E6%9C%89%E5%A4%9A%E5%8F%B0%E4%B8%BB%E6%9C%BA%E4%B8%8D%E9%9C%80%E8%A6%81%E4%BF%AE%E6%94%B9%E8%B7%B3%E8%BF%87%E8%AF%A5%E6%AD%A5%E9%AA%A4"><span class="toc-number">4.3.1.</span> <span class="toc-text"> 4. 修改 nacos 启动脚本 startup.sh 让其能够进行不同端口的启动。(为了能在一台主机上启动多个实例对象，模拟 3 台主机。有多台主机不需要修改，跳过该步骤)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AEnginx"><span class="toc-number">4.4.</span> <span class="toc-text"> 配置 NGINX</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8"><span class="toc-number">4.5.</span> <span class="toc-text"> 启动</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8nacos"><span class="toc-number">4.5.1.</span> <span class="toc-text"> 启动 Nacos:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8nginx"><span class="toc-number">4.5.2.</span> <span class="toc-text"> 启动 nginx</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95-3"><span class="toc-number">4.5.3.</span> <span class="toc-text"> 测试:</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B0%86%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E5%88%B0nacos%E9%9B%86%E7%BE%A4"><span class="toc-number">4.6.</span> <span class="toc-text"> 将微服务注册到 Nacos 集群</span></a></li></ol></li></ol>
      </div>
      <div class="related panel pjax" data-title="系列文章">
        <ul>
          <li class="active"><a href="/blog/2020/12/24/Nacos/" rel="bookmark" title="Cloud Alibaba Nacos">Cloud Alibaba Nacos</a></li>
        </ul>
      </div>
      <div class="overview panel" data-title="站点概览">
        <div class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="image" itemprop="image" alt="杜朝宇"
      data-src="/blog/images/touxiang.jpg">
  <p class="name" itemprop="name">杜朝宇</p>
  <div class="description" itemprop="description">个人博客&编程笔记</div>
</div>

<nav class="state">
    <div class="item posts">
      <a href="/blog/archives/">
        <span class="count">8</span>
        <span class="name">文章</span>
      </a>
    </div>
    <div class="item categories">
      <a href="/blog/categories/">
        <span class="count">4</span>
        <span class="name">分类</span>
      </a>
    </div>
    <div class="item tags">
      <a href="/blog/tags/">
        <span class="count">2</span>
        <span class="name">标签</span>
      </a>
    </div>
</nav>

<div class="social">
</div>

<ul class="menu">
  
    
  <li class="item">
    <a href="/blog/" rel="section"><i class="ic i-home"></i>首页</a>
  </li>

    
  <li class="item">
    <a href="/blog/about/" rel="section"><i class="ic i-user"></i>关于</a>
  </li>

        
  <li class="item dropdown">
      <a href="javascript:void(0);"><i class="ic i-feather"></i>文章</a>
    <ul class="submenu">

        
  <li class="item">
    <a href="/blog/archives/" rel="section"><i class="ic i-list-alt"></i>归档</a>
  </li>

        
  <li class="item">
    <a href="/blog/categories/" rel="section"><i class="ic i-th"></i>分类</a>
  </li>

        
  <li class="item">
    <a href="/blog/tags/" rel="section"><i class="ic i-tags"></i>标签</a>
  </li>

  </ul>
        
  <li class="item dropdown">
      <a href="javascript:void(0);"><i class="ic i-magic"></i>链环</a>
    <ul class="submenu">

        
  <li class="item">
    <a href="/blog/friends/" rel="section"><i class="ic i-heart"></i>友链</a>
  </li>

  </ul>

</ul>

      </div>
    </div>
  </div>

  <ul id="quick">
    <li class="prev pjax">
        <a href="/blog/2020/12/24/Spring%20Cloud%20Stream/" rel="prev" title="上一篇"><i class="ic i-chevron-left"></i></a>
    </li>
    <li class="up"><i class="ic i-arrow-up"></i></li>
    <li class="down"><i class="ic i-arrow-down"></i></li>
    <li class="next pjax">
        <a href="/blog/2020/12/24/Spring%20Cloud%20sleuth/" rel="next" title="下一篇"><i class="ic i-chevron-right"></i></a>
    </li>
    <li class="percent"></li>
  </ul>
</div>


        </div>
        <div class="dimmer"></div>
      </div>
    </main>
    <footer id="footer">
      <div class="inner">
        <div class="widgets">
          
<div class="rpost pjax">
  <h2>随机文章</h2>
  <ul>
      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B0/" title="分类于 开源项目笔记">开源项目笔记</a>
</div>

    <span><a href="/blog/2020/12/23/moti%E5%BC%80%E6%BA%90%E5%AD%A6%E5%88%B0%E7%9A%84/" title="moti">moti</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/Spring-Cloud-Alibaba/" title="分类于 Spring Cloud Alibaba">Spring Cloud Alibaba</a>
</div>

    <span><a href="/blog/2020/12/24/Nacos/" title="Cloud Alibaba Nacos">Cloud Alibaba Nacos</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Hystrix/" title="Hystrix">Hystrix</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Config+Bus/" title="Spring Config+Bus">Spring Config+Bus</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Cloud%20Stream/" title="Spring Cloud Stream">Spring Cloud Stream</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/java/" title="分类于 java">java</a>
</div>

    <span><a href="/blog/2020/12/23/Druid%E9%85%8D%E7%BD%AE/" title="Druid配置">Druid配置</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/gateway/" title="gateway">gateway</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Cloud%20sleuth/" title="Spring Cloud sleuth">Spring Cloud sleuth</a></span>
  </li>

  </ul>
</div>
<div>
  <h2>最新评论</h2>
  <ul class="leancloud-recent-comment"></ul>
</div>

        </div>
        <div class="status">
  <div class="copyright">
    
    &copy; 2010 – 
    <span itemprop="copyrightYear">2020</span>
    <span class="with-love">
      <i class="ic i-sakura rotate"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">杜朝宇 @ blog</span>
  </div>
  <div class="count">
    <span class="post-meta-item-icon">
      <i class="ic i-chart-area"></i>
    </span>
    <span title="站点总字数">42k 字</span>

    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="ic i-coffee"></i>
    </span>
    <span title="站点阅读时长">38 分钟</span>
  </div>
  <div class="powered-by">
    基于 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & Theme.<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2FtZWhpbWUvaGV4by10aGVtZS1zaG9rYQ==">Shoka</span>
  </div>
</div>

      </div>
    </footer>
  </div>
<script data-config type="text/javascript">
  var LOCAL = {
    path: '2020/12/24/Nacos/',
    favicon: {
      show: "（●´3｀●）やれやれだぜ",
      hide: "(´Д｀)大変だ！"
    },
    search : {
      placeholder: "文章搜索",
      empty: "关于 「 ${query} 」，什么也没搜到",
      stats: "${time} ms 内找到 ${hits} 条结果"
    },
    valine: true,fancybox: true,copyright: '复制成功，转载请遵守 <i class="ic i-creative-commons"></i>BY-NC-SA 协议。',
    ignores : [
      function(uri) {
        return uri.includes('#');
      },
      function(uri) {
        return new RegExp(LOCAL.path+"$").test(uri);
      }
    ]
  };
</script>

<script src="https://cdn.polyfill.io/v2/polyfill.js"></script>

<script src="//cdn.jsdelivr.net/combine/npm/pace-js@1.0.2/pace.min.js,npm/pjax@0.2.8/pjax.min.js,npm/whatwg-fetch@3.4.0/dist/fetch.umd.min.js,npm/animejs@3.2.0/lib/anime.min.js,npm/algoliasearch@4/dist/algoliasearch-lite.umd.js,npm/instantsearch.js@4/dist/instantsearch.production.min.js,npm/lozad@1/dist/lozad.min.js,npm/quicklink@2/dist/quicklink.umd.js"></script>

<script src="/blog/js/app.js?v=0.2.5"></script>




</body>
</html>
